﻿using GN.Pay.TradeChannel.Gyb.Response;
using GN.Pay.Utils;
using GN.Pay.Utils.Net.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.Gyb.Request
{
    /// <summary>
    /// 退款请求
    /// </summary>
    [Serializable]
    public class RefundRequest : ARequest
    {
        public RefundRequest(ITradeChannel channel,
            IRefundApplyInfo apply,
            GybConfigure config)
            : base(config)
        {
            if (apply == null)
            {
                channel.ThrowChannelValidationException("请求的 apply 为 null");
            }
            MercDtTm = apply.ApplyTime.ToString("yyyyMMddHHmmss");
            TermSsn = apply.OrderNo;
            OAcqSsn = apply.TradeNo;
            OSttDate = apply.PayTime.ToString("yyyyMMdd");
            TermCode = "00000000";
            TranAmt = Math.Round(apply.ApplyMoney, 2);
            Remark1 = FixedLengthString(apply.RefundReason, 22);
            Remark2 = FixedLengthString(apply.ExtraParam, 99);
        }

        public override string TranAbbr
        {
            get
            {
                return TradeCommand.IPSR;
            }
        }

        /// <summary>
        /// 商户日期时间   必须提供，商户的交易日期时间yyyyMMddhhMMss 长度：14
        /// </summary>
        public string MercDtTm { get; set; }

        /// <summary>
        /// 订单号  不能重复 长度：20
        /// </summary>
        public string TermSsn { get; set; }

        /// <summary>
        /// 原交易清算日期，原交易的清算日期，支付交易不用填值，撤销退货需填， 长度：8
        /// </summary>
        public string OSttDate { get; set; }

        /// <summary>
        /// 原网关流水 原交易的网关流水，支付交易不用填值，撤销退货需填 长度：12
        /// </summary>
        public string OAcqSsn { get; set; }
        /// <summary> 
        /// 终端号	可以全为0  长度：8
        /// </summary>
        public string TermCode { get; set; }

        /// <summary>
        /// 交易金额 长度：12
        /// </summary>
        public decimal TranAmt { get; set; }

        /// <summary>
        /// 交易备注一，该字段将在对账文件中提供 长度：22
        /// </summary>
        public string Remark1 { get; set; }

        /// <summary>
        /// 交易备注二，该字段将在对账文件中提供 长度：99
        /// </summary>
        public string Remark2 { get; set; }

        /// <summary>
        /// 生成退款请求并得到响应
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="config"></param>
        /// <param name="apply"></param>
        /// <returns></returns>
        public RefundResponse Build(ITradeChannel channel,
               GybConfigure config,
               IRefundApplyInfo apply)
        {
            var plain = BuildPlain();
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic["transName"] = this.TranAbbr;
            dic["Plain"] = plain;
            dic["Signature"] = SecurityUtils.Sign(config.ConfigureInfo, plain);

            string requestContent = BuildXML(dic, config);

            HttpClient client = new HttpClient();
            client.Encoding = config.Encoding;
            client.ContentType = "text/xml";
            var response = client.PostString(new Uri(config.GatewayUrl), requestContent);

            return new RefundResponse(response, channel, config, apply);
        }
    }
}
